package com.net.core.models.user.controller;

import com.net.core.api.req.GetUserReq;
import com.net.core.api.req.LoginReq;
import com.net.core.core.reuslt.GeneratorResult;
import com.net.core.core.reuslt.Response;
import com.net.core.models.BaseController;
import com.net.core.models.bean.User;
import com.net.core.models.user.service.UserService;
import com.net.core.utils.CurrentCache;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("user")
@Api(tags = "用户接口")
@Slf4j
public class UserController extends BaseController {

    @Resource
    UserService userService;

    @PostMapping("get")
    @ApiOperation(value = "获取用户")
    public Response getUser(@RequestBody GetUserReq rep) {
        List<User> users = userService.getUser(rep.getKey(), getUser());
        return GeneratorResult.genSuccessMessage(users);
    }

    @PostMapping("login")
    @ApiOperation(value = "用户登录")
    public Response login(@RequestBody LoginReq rep) {
        User user = userService.login(rep.getUsername(), rep.getPassword());
        currentCache.putUser(user);
        return GeneratorResult.genSuccessMessage(user);
    }
}
